package demoMod.gdxform.ui;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;

import java.util.List;

public class GRadioButton extends GCheckBox {
    private List<GRadioButton> group;

    public GRadioButton(float x, float y, float width, float height, BitmapFont font) {
        super(x, y, width, height, font);
    }

    public GRadioButton(float x, float y, float width, float height) {
        super(x, y, width, height);
    }

    @Override
    protected Texture initBgTexture(Pixmap pixmap) {
        pixmap.setColor(Color.WHITE);
        pixmap.fillCircle(pixmap.getWidth()  / 2, pixmap.getHeight() / 2, pixmap.getWidth() / 2);
        return new Texture(pixmap);
    }

    @Override
    protected Texture initUnselectedTexture(Pixmap pixmap) {
        return new Texture(pixmap);
    }

    @Override
    protected Texture initSelectedTexture(Pixmap pixmap) {
        pixmap.setColor(Color.GREEN);
        pixmap.fillCircle(pixmap.getWidth()  / 2, pixmap.getHeight() / 2, Math.round((pixmap.getWidth() / 2.0F) * 0.7F));
        return new Texture(pixmap);
    }

    @Override
    protected Texture initPartialSelectedTexture(Pixmap pixmap) {
        return initUnselectedTexture(pixmap);
    }

    @Override
    protected Texture initDisabledTexture(Pixmap pixmap) {
        pixmap.setColor(Color.GRAY);
        pixmap.fillCircle(pixmap.getWidth()  / 2, pixmap.getHeight() / 2, Math.round((pixmap.getWidth() / 2.0F) * 0.7F));
        return new Texture(pixmap);
    }

    @Override
    public boolean clickUp(int screenX, int screenY, int button) {
        if (!this.visible()) return false;
        if (clicked && enabled()) {
            clicked = false;
            if (screenX >= getX(true) && screenX <= getX(true) + getWidth() &&
                    Gdx.graphics.getHeight() - screenY >= getY(true) && Gdx.graphics.getHeight() - screenY <= getY(true) + getHeight()) {
                setState(1);
                onClick();
            }
            return true;
        }
        return false;
    }

    @Override
    public void setState(int state) {
        this.state = state - 1;
        changeState(2);
        if (this.group != null && this.state == 1) {
            for (GRadioButton radioButton : this.group) {
                if (radioButton != this) {
                    radioButton.setState(0);
                }
            }
        }
    }

    @Override
    public void setEnabled(boolean enabled) {
        super.setEnabled(enabled);
        if (this.group != null) {
            for (GRadioButton radioButton : this.group) {
                if (radioButton != this) {
                    radioButton.setEnabled(enabled);
                }
            }
        }
    }

    public List<GRadioButton> getGroup() {
        return group;
    }

    public void setGroup(List<GRadioButton> group) {
        this.group = group;
        if (!this.group.contains(this)) {
            this.group.add(this);
        }
    }
}
